﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Configuration;
using System.Text;
using webapp4U.BOL;


namespace webapp4U.UI
{
    public partial class WebMaster_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }


        protected void btSend_Click(object sender, EventArgs e)
        {
            //StringBuilder emailList = new StringBuilder(string.Empty);

            string emailList = "";
            int stt = 0;
            if (ddlGroup.SelectedItem.Value != "")
            {
                // send cho nhom nao
                if (ddlGroup.SelectedItem.Value == "1") // NewsLetter
                {
                    List<webapp4U.BOL.NewsLetter> obj = webapp4U.BOL.NewsLetter.GetAllNewsLetter();
                    foreach (webapp4U.BOL.NewsLetter fnewsletter in obj)
                    {
                        if (emailList != "")
                            emailList = emailList + ",";

                        emailList = emailList + fnewsletter.Email;
                        stt = stt + 1;


                        if(stt == 100 && obj.Count > 100)
                        {
                            SendMail(emailList.ToString());
                            emailList = "";
                            stt = 0;
                        }
                        else
                            SendMail(emailList.ToString());
                    }


                }
                else if (ddlGroup.SelectedItem.Value == "2") // JobSeeker
                {
                    List<webapp4U.BOL.User> obj = webapp4U.BOL.User.GetUserByRole(Globals.Settings.AppRoles.KhachHang);
                    foreach (webapp4U.BOL.User fnewsletter in obj)
                    {
                        if (emailList.ToString() != "")
                            emailList = emailList + ",";
                        emailList = emailList + fnewsletter.Email;

                        stt = stt + 1;
                        if (stt == 100 && obj.Count>100)
                        {
                            SendMail(emailList.ToString());
                            emailList = "";
                            stt = 0;
                        }
                        else
                            SendMail(emailList.ToString());
                    }
                }

            }

            PanelMsg.Visible = true;
            lblMsg.Text = ResourceManager.GetString("NewsLetter_SentSuccessfully");
        }


        public bool SendMail(string mailTo)
        {
            try
            {
                SmtpMail.SmtpServer = ConfigurationManager.AppSettings["SmtpServer"].ToString();
                MailMessage MailMsg = new MailMessage();
                MailMsg.To = mailTo;
                MailMsg.From = ConfigurationManager.AppSettings["EmailFrom"].ToString();
                MailMsg.Subject = txtTitle.Text;
                string strBody = txtBody.Text;
                strBody = strBody + "<br><br> Nếu bạn không muốn nhận NewsLetter từ lần sau thì click vào link sau: ";
                strBody = strBody + ConfigurationManager.AppSettings["Domain"].ToString() + "/RejectNewsLetter.aspx";
                MailMsg.Body = strBody;
                MailMsg.BodyFormat = MailFormat.Html;
                MailMsg.BodyEncoding = System.Text.Encoding.UTF8;
                SmtpMail.Send(MailMsg);
                return true;
            }
            catch { return false; }
        }
    }
}